package com.nlp.visualization.controller.spring.config;

import com.nlp.visualization.service.IJWTService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description: 用于登陆拦截
 * @date 2018/5/11下午9:54
 */
public class LoginHandlerIntercepter implements HandlerInterceptor {


    @Autowired
    private IJWTService ijwtService;

    private static final String role = "admin";

    private Logger logger = LoggerFactory.getLogger(LoginHandlerIntercepter.class);

    /**
     * 在进入Handler方法之前执行了，使用于身份认证，身份授权，登陆校验等，比如身份认证，用户没有登陆，拦截不再向下执行，返回值为 false ，即可实现拦截；否则，返回true时，拦截不进行执行；
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String requestURI = request.getRequestURI();
        if (requestURI.indexOf("/admin") > 0) {
            //说明处在登陆的页面
            String jwt;
            jwt = request.getParameter("access_token");
            if (ijwtService.isJwtValid(jwt, role)) {
                //登陆成功的用户
                logger.info("用户已登陆");
                return true;
            } else {
                //没有登陆，转向登陆界面
                logger.error("用户未登陆");
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * 进入Handler方法之后，返回ModelAndView之前执行，使用场景从ModelAndView参数出发，比如，将公用的模型数据在这里传入到视图，也可以统一指定显示的视图等；
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 在执行Handler完成后执行此方法，使用于统一的异常处理，统一的日志处理等；
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
